<template>
  <div>
    <div id="area" :style="{ width: offsetwidth + 'px', height: '300px'}"></div>
  </div>
</template>

<script>
import echarts from 'echarts'
import { temperatureAlllist } from '@/api/yi-qing-fang-kong'
// import mockData from './mockData'

export default {
  name: 'FeverEchart',
  data () {
    return {
      chartData: [],
      offsetwidth: document.documentElement.clientWidth - 300
    }
  },
  mounted () {
    this.chart()
  },
  methods: {
    chart () {
      temperatureAlllist({ user_id: this.$route.query.id }).then(ret => {
        if (ret.data.errcode === 0) {
          this.chartData = ret.data.data
          // console.log('---', this.chartData)
          let option = {
            title: {
              text: ''
            },
            textStyle: {
              color: '#C5C5C5'
            },
            tooltip: {
              trigger: 'axis',
              backgroundColor: 'rgba(50,50,50,0.5)',
              formatter: function (params) {
                params = params[0]
                const date = new Date(params.value.measure_time)
                return date.getFullYear().toString().padStart(4, '0') + '-' +
                  (date.getMonth() + 1).toString().padStart(2, '0') + '-' +
                  date.getDate().toString().padStart(2, '0') + '<br>' +
                  date.getHours().toString().padStart(2, '0') + ':' +
                  date.getMinutes().toString().padStart(2, '0') + ':' +
                  date.getSeconds().toString().padStart(2, '0') + '<br>' +
                  params.value.temperature + '℃'
              }
            },
            dataset: {
              dimensions: ['measure_time', 'temperature'],
              source: []
            },
            yAxis: {
              name: '温度/℃',
              min: 36,
              max: 41,
              type: 'value',
              axisLine: {
                lineStyle: {
                  color: '#C5C5C5' // 轴及其文字颜色
                },
                show: true
              }
            },
            xAxis: {
              name: '时间',
              // min: this.chartData[0].measure_time,
              boundaryGap: true,
              type: 'time',
              axisLine: {
                lineStyle: {
                  color: '#C5C5C5' // 轴及其文字颜色
                },
                show: true
              },
              splitLine: {
                show: true
              },
              axisLabel: {
                formatter: function (val) {
                  const date = new Date(val)
                  return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
                }
              }
            },
            series: [
              {
                name: '温度',
                type: 'line',
                smooth: true,
                symbolSize: 8,
                label: {
                // normal: {
                //   show: true,
                //   position: 'inside',
                //   color: ['#fff']
                // }
                },
                itemStyle: {
                // 通常情况下：
                  normal: {
                    color: ['#096FEB']
                  },
                  areaStyle: {},
                  // 鼠标悬停时：
                  emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                  }
                }
              }
            ]
          }
          this.chartData.forEach(item => {
            option.dataset.source.push(item)
          })
          let myChart = echarts.init(document.getElementById('area'))
          myChart.setOption(option, true)
        } else {
          this.$Message.error({
            content: '数据获取失败,\n' + ret.data.errmsg
          })
        }
      })
    }
  }
}
</script>

<style scoped>
  .header {
    height: 30px;
  }

  .icon {
    font-size: 28px;
    font-weight: 600;
    margin-right: 5px;
  }

  .title {
    font-size: 16px;
    font-weight: 600;
    line-height: 30px;
  }
</style>
